<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\stage\controller;
use app\api\logic\Sms;
use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class Min extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'php_min';

    /**
     * 用户列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '商家入驻列表';
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        foreach (['username'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db->order('status add'));
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        $data = ToolsService::arr2table($data);
    }
    /**
     * 编辑商家
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function edit()
    {
        $this->title = '编辑商家';
        $result = Db::table('php_mcate')->select();
        $this->assign('data',$result);
        return $this->_form($this->table, 'form');
    }

    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");

    }

    /**
     * 拒绝入驻
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {

        $aid = $_GET['id'];
        //根据id获取
        $data = Db::name('php_min')->where(['id' => $aid])->find();
        //判断是否已经是商家
        $is_store = Db::name('php_store_member')->where(['store_uid' => $data['store_uid']])->find();
        if($is_store) $this->error("该用户已经是商家");
        //发送短信通知&&获取电话号码 //开始发送短信
        $result = \app\api\logic\Sms::sendSMS("{$data['store_mobile']}","9740",['未通过']);
        if($result != 'ok') $this->error("操作失败，请稍候再试！");
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 同意入驻
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $aid = $_GET['id'];
        //根据id获取
        $data = Db::name('php_min')->where(['id' => $aid])->find();
        unset($data['create_at']);
        unset($data['status']);
        //判断是否已经是商家
        $is_store = Db::name('php_store_member')->where(['store_uid' => $data['store_uid']])->find();
        if($is_store) $this->error("该用户已经是商家");
        //存入到商家表
        $innsert_data = Db::name('php_store_member')->insertGetId($data);
        if($innsert_data){
            Db::name('php_min')->where(['id' => $aid])->update(['status' => 0]);
            Db::name('php_member')->where(['id' => $data['store_uid']])->update(['store_id' => $innsert_data]);
            //发送短信通知&&获取电话号码 //开始发送短信
            $result = \app\api\logic\Sms::sendSMS("{$data['store_mobile']}","9740",['已通过']);
            if($result != 'ok') $this->error("操作失败，请稍候再试！");
            $this->success("操作成功！", '');
        }else{
            $this->error("操作失败，请稍候再试！");
        }
    }

    /**
     * 添加成功回跳处理
     * @param bool $result
     */
    protected function _form_result($result)
    {
        if ($result !== false) {
            list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('stage/member/index')];
            $this->success('数据保存成功！', "{$base}#{$url}?spm={$spm}");
        }
    }
}
